//
//  JXPositionChooseViewController.m
//  JXAddressApp
//
//  Created by zhhw_home on 2018/11/4.
//  Copyright © 2018年 zhhw_home. All rights reserved.
//

#import "JXPositionChooseViewController.h"
#import "TMapView.h"
#import "JXAnnotation.h"
#import "QMUICommonDefines.h"

@interface JXPositionChooseViewController ()
<
TMapViewDelegate
>

@property (nonatomic, strong) TMapView  *mapView;
@property (nonatomic, strong) UIButton  *selectedButton;

@property (nonatomic, strong) JXAnnotation *anotation;

@end

@implementation JXPositionChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择位置";

    _mapView = [[TMapView alloc]initWithFrame:self.view.frame];
    // 设置地图回调类
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectedButton.backgroundColor = [UIColor whiteColor];
    _selectedButton.layer.cornerRadius = 3.f;
    _selectedButton.layer.masksToBounds = YES;
    _selectedButton.frame = CGRectMake(20, SCREEN_HEIGHT-150, 44, 44);
    [_selectedButton setImage:[UIImage imageNamed:@"mark"] forState:UIControlStateNormal];
    [_selectedButton addTarget:self action:@selector(mark) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_selectedButton];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)mark
{
    if (_anotation) {
        [_mapView removeAnnotation:_anotation];
        _anotation = nil;
    }
    
    CLLocationCoordinate2D center = _mapView.centerCoordinate;
    
    _anotation = [[JXAnnotation alloc] init];
    _anotation.coordinate = center;
    
    [_mapView addAnnotation:_anotation];
}

- (void)save
{
    if (_chooseBlock) {
        _chooseBlock(_anotation.coordinate);
    }
}

@end
